﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.RemoveItems
{
    public partial class RemoveItemsEdit : System.Web.UI.Page
    {
        public string ID
        {
            set
            {
                ViewState["ID"] = value;
            }
            get
            {
                return ViewState["ID"] != null ? ViewState["ID"].ToString() : Guid.NewGuid().ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserPermissionBll userPermissionBll = new UserPermissionBll();
                if (!userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.RemoveItems))
                {
                    Response.Write("您没有权限使用该功能");
                    Response.End();
                    return;
                }
                this.ID = Request.QueryString["id"];
                InitControlData(this.ID);
            }
        }

        private void InitControlData(string id)
        {
            string sql = @"SELECT * FROM dbo.t5715_RemoveItems WHERE ID = @ID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        txt1.Text = reader["ItemCode"] != null ? reader["ItemCode"].ToString() : string.Empty;
                        txt2.Text = reader["ItemName"] != null ? reader["ItemName"].ToString() : string.Empty;
                        txt3.Text = reader["Code"] != null ? reader["Code"].ToString() : string.Empty;
                        txt4.Text = reader["Count"] != null ? reader["Count"].ToString() : string.Empty;
                        txt5.Text = reader["Dispatch"] != null ? reader["Dispatch"].ToString() : string.Empty;
                        txt6.Text = reader["Missile"] != null ? reader["Missile"].ToString() : string.Empty;
                        txt7.Text = reader["Process"] != null ? reader["Process"].ToString() : string.Empty;
                        txt8.Text = reader["Remark"] != null ? reader["Remark"].ToString() : string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpdateData(string id, string txt1, string txt2, string txt3, string txt4
            ,string txt5, string txt6, string txt7, string txt8 )
        {
            string sql_select = @"SELECT COUNT(ID) FROM dbo.t5715_RemoveItems WHERE ID = @ID";
            string sql_insert = @"INSERT INTO dbo.t5715_RemoveItems
                                       ([ID]
                                       ,[ItemCode]
                                       ,[ItemName]
                                       ,[Code]
                                       ,[Count]
                                       ,[Dispatch]
                                       ,[Missile]
                                       ,[Process]
                                       ,[Remark]
                                       ,[CreateDate])
                                 VALUES
                                       (@ID
                                       ,@ItemCode
                                       ,@ItemName
                                       ,@Code
                                       ,@Count
                                       ,@Dispatch
                                       ,@Missile
                                       ,@Process
                                       ,@Remark
                                       ,GETDATE())";
            string sql_update = @"UPDATE dbo.t5715_RemoveItems
                                   SET ItemCode = @ItemCode
                                       ,ItemName = @ItemName
                                       ,Code = @Code
                                       ,Count = @Count
                                       ,Dispatch = @Dispatch
                                       ,Missile = @Missile
                                       ,Process = @Process
                                       ,Remark = @Remark
                                 WHERE ID = @ID";

            try
            {

                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_select);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);
                string count = database.ExecuteScalar(dbCommand).ToString();

                if (count.Equals("0"))
                {
                    dbCommand = database.GetSqlStringCommand(sql_insert);
                }
                else
                {
                    dbCommand = database.GetSqlStringCommand(sql_update);
                }

                database.AddInParameter(dbCommand, "@ID", DbType.String, id);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, txt1);
                database.AddInParameter(dbCommand, "@ItemName", DbType.String, txt2);
                database.AddInParameter(dbCommand, "@Code", DbType.String, txt3);
                database.AddInParameter(dbCommand, "@Count", DbType.String, txt4);
                database.AddInParameter(dbCommand, "@Dispatch", DbType.String, txt5);
                database.AddInParameter(dbCommand, "@Missile", DbType.String, txt6);
                database.AddInParameter(dbCommand, "@Process", DbType.String, txt7);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, txt8);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateData(this.ID, txt1.Text.Trim(), txt2.Text.Trim(), txt3.Text.Trim(), txt4.Text.Trim()
                    , txt5.Text.Trim(), txt6.Text.Trim(), txt7.Text.Trim(), txt8.Text.Trim());                
                Response.Redirect("RemoveItemsInfo.aspx");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>"); 
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("RemoveItemsInfo.aspx");
        }
    }
}